using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;

namespace SBPweb.Controls.Windows.Tab
{
	/// <summary>
	/// Class of TabHeader
	/// </summary>
	[ToolboxItem(false)]
	public partial class TabHeader : SBPweb.Controls.Windows.Common.UserControl, ISkinnable
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Control mParent;
		private Dictionary<Control, TabPane> mPanes;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of TabHeader
		/// </summary>
		public TabHeader()
		{
			mPanes = new Dictionary<Control, TabPane>();

			SetStyle();
			InitializeComponent();
			RegisterEventHandlers();
			PositionAllTabPanes();
		}

		/// <summary>
		/// Constructor of TabHeader
		/// </summary>
		/// <param name="parent">parent tabcontrol</param>
		public TabHeader(TabControl parent)
		{
			mParent = parent;
			mPanes = new Dictionary<Control, TabPane>();

			SetStyle();
			InitializeComponent();
			RegisterEventHandlers();
			PositionAllTabPanes();
		}

		/// <summary>
		/// Constructor of TabHeader
		/// </summary>
		/// <param name="parent">tabcontrol parent</param>
		/// <param name="skin">skin</param>
		public TabHeader(TabControl parent, Skin skin)
		{
			mParent = parent;
			mPanes = new Dictionary<Control, TabPane>();
			mSkin = skin;
			
			SetStyle();
			InitializeComponent();
			RegisterEventHandlers();
			PositionAllTabPanes();
		}

		private void SetStyle()
		{
			this.BackColor = Color.Transparent;
			base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
		}

		private void RegisterEventHandlers()
		{
			if (this.Parent != null)
			{
				this.Parent.ControlAdded += new ControlEventHandler(Parent_ControlAdded);
				this.Parent.ControlRemoved += new ControlEventHandler(Parent_ControlRemoved);
			}
		}

		private void PositionAllTabPanes()
		{
			int i = 0;
			foreach (TabPane tp in mPanes.Values)
			{
				tp.Left = CalculateTabPaneLocation(i++);
			}
			Invalidate(true);
		}

		private int CalculateTabPaneLocation(int index)
		{
			int ret = 5;
			int width = 0;
			int i = 0;
			foreach (TabPane tp in mPanes.Values)
			{
				if (i > 0)
				{
					ret += width - 5;
				}
				width = tp.Width;
				if (i++ == index)
				{
					break;
				}
			}

			return ret;
		}
		#endregion

		#region Properties

		/// <summary>
		/// Parent control
		/// </summary>
		internal new Control Parent
		{
			get
			{
				return mParent;
			}
			set
			{
				mParent = value;
				RegisterEventHandlers();
			}
		}

		#endregion

		#region Custom draw

		internal new void Paint(PaintEventArgs e)
		{
			this.Invalidate(true);
			this.OnPaint(e);
		}

		/// <summary>
		/// On paint
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			this.Height = (this.Parent as TabControl).ItemSize.Height + 4;
			base.OnPaint(e);
		}

		/// <summary>
		/// On background paint
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnPaintBackground(PaintEventArgs e)
		{
			Rectangle rect = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height - 2);
			PaintEventArgs pea = new PaintEventArgs(e.Graphics, rect);
			base.OnPaintBackground(pea);
//			rect = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Height - 2, this.ClientRectangle.Width, 1);
//			e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), rect);
		}

		#endregion

		#region Events

		private void Parent_ControlAdded(object sender, ControlEventArgs e)
		{
			TabPane tp = new TabPane(e.Control as TabPage);
			tp.SizeChanged += new EventHandler(tp_SizeChanged);
			tp.Resize += new EventHandler(tp_Resize);
			this.mPanes.Add(e.Control, tp);
			this.Controls.Add(tp);
			PositionAllTabPanes();
		}

		private void tp_SizeChanged(object sender, EventArgs e)
		{
			PositionAllTabPanes();
		}

		private void tp_Resize(object sender, EventArgs e)
		{
			PositionAllTabPanes();
		}

		private void Parent_ControlRemoved(object sender, ControlEventArgs e)
		{
			TabPane tp = this.mPanes[e.Control];
			this.Controls.Remove(tp);
			this.mPanes.Remove(tp);
			PositionAllTabPanes();
		}

		#endregion


		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public override bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				Invalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public override Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				Invalidate();
			}
		}

		#endregion
	}
}
